package com.issc.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class ExamTest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入考题id：");
		int quesid = sc.nextInt();
		System.out.println("请输入学生答案：");
		String stuanswer = sc.next();
		//单选，填空，判断
//		try {
//			getscore(quesid,stuanswer);
//		} catch (ClassNotFoundException | SQLException e) {
//			e.printStackTrace();
//		}
		//多选
		try {
			getscore2(quesid,stuanswer);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void getscore(int quesid,String stuanswer) throws ClassNotFoundException, SQLException{
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/homework?useUnicode=true&amp;characterEncoding=utf-8";
		Connection conn=DriverManager.getConnection(url,"root","root123");
		
		String sql = "select jjq_cmd(?,?) score, answer from question a where id=?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, quesid);
		ps.setString(2, stuanswer);
		ps.setInt(3, quesid);
		
		ResultSet rs = ps.executeQuery();
		
		while(rs.next()){
			System.out.println(rs.getString("score"));
			System.out.println(rs.getString("answer"));
		}
		rs.close();
		ps.close();
		conn.close();
	}
	
	public static void getscore2(int quesid,String stuanswer) throws ClassNotFoundException, SQLException{
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/homework?useUnicode=true&amp;characterEncoding=utf-8";
		Connection conn=DriverManager.getConnection(url,"root","root123");
		
		String sql = "select jjq_duoxuan(?,?) score, answer from question a where id=?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, quesid);
		ps.setString(2, stuanswer);
		ps.setInt(3, quesid);
		
		ResultSet rs = ps.executeQuery();
		
		while(rs.next()){
			System.out.println(rs.getString("score"));
			System.out.println(rs.getString("answer"));
		}
		rs.close();
		ps.close();
		conn.close();
	}

}
